import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:speed_random/common/common_fuc.dart';
import 'package:speed_random/core/http/api_app_info.dart';
import 'package:speed_random/global_data.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../myapp.dart';
import 'state.dart';

class SettingLogic extends GetxController {
  final state = SettingState();

  late Function(Color) onSave;
  @override
  void onInit() {
    super.onInit();
   // final args = Get.arguments as Map;

   // onSave = args['onSave'] as Function(Color);
    loadSettings();


    if(globalData.appInfo.soft?.id==null){
      ApiAppInfo.get();
    }

  }




  void loadSettings() async {
    final prefs = await SharedPreferences.getInstance();
    state.currentMs.value = prefs.getBool('appLookMs')??false;
    state.sound_path.value = prefs.getString('sound_path')??"";
    state.currentLanguage.value = prefs.getString('app_language') ?? 'zh';
    state.currentLanguage_name.value = prefs.getString('app_language_name') ?? '中文';
    state.currentTheme.value = ThemeMode.values[prefs.getInt('app_theme') ?? 0];

    state.sound_path_name.value=globalData.soundPathName;
    state.sound_success_path_name.value=globalData.soundSuccessPathName;


  }


  void saveSoundSetting() async {

    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('soundPath', globalData.soundPath);
    await prefs.setString('soundSuccessPath', globalData.soundSuccessPath);

    await prefs.setString('soundPathName', globalData.soundPathName);
    await prefs.setString('soundSuccessPathName', globalData.soundSuccessPathName);

  }



  void changeLanguage() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('app_language', state.currentLanguage.value);
    await prefs.setString('app_language_name', state.currentLanguage_name.value);
    Get.updateLocale(Locale(state.currentLanguage.value));
  }

  void changeTheme() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt('app_theme', state.currentTheme.value.index);
    await prefs.setBool('appLookMs', state.currentMs.value);
   // globalData.appLookMs=state.currentMs.value;
    Get.changeThemeMode(state.currentTheme.value);
  }
  void changeThemeBg() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('app_theme_bg', state.currentThemeBg.value);
    globalData.bgColor=commonFuc.hexToColor(state.currentThemeBg.value);
    onSave(globalData.bgColor);
  }




}
